﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace chatSDKApp.Forms.ChatRoom
{
    public partial class FrmViewImages : Office2007Form
    {
        public FrmViewImages()
        {
            InitializeComponent();
            this.EnableGlass = false;//使用donetBar样式
        }
        // <summary>
        /// 控件在父控件中居中
        /// </summary>
        /// <param name="ctr">要设置的控件</param>
        /// <param name="isLR">左右居中</param>
        /// <param name="isUD">上下居中</param>
        /// <returns></returns>
        public static bool CenterCtr(Control ctr, bool isLR, bool isUD)
        {
            Control pCtr = ctr.Parent;
            int x = isLR ? ((pCtr.Width - ctr.Width) / 2) : ctr.Location.X;
            int y = isUD ? ((pCtr.Height - ctr.Height) / 2) : ctr.Location.Y;
            ctr.Location = new System.Drawing.Point(x , y );
            return true;
        }
        public void pictureBox_MaxView(object sender, MouseEventArgs e)
        {
            this.pic_MaxImage.Anchor = AnchorStyles.None;
            this.pic_MaxImage.SizeMode = PictureBoxSizeMode.StretchImage;//自适应长宽
            PictureBox pic = (PictureBox)sender;
            var img = pic.Image.Clone();
            this.pic_MaxImage.Image = (Image)img;

            //图片控件的宽高
            double w = this.pic_MaxImage.Size.Width;
            double h = this.pic_MaxImage.Size.Height;

            //图片的宽高
            int imageWidth = this.pic_MaxImage.Image.Size.Width;
            int imageHeight = this.pic_MaxImage.Image.Size.Height;

            //根据Images的大小设置picBox的大小
            if (imageHeight <300 || imageWidth < 300)
            {
                //放大1.2倍数
                this.pic_MaxImage.Height = (int)(imageHeight * 1.2);
                this.pic_MaxImage.Width = (int)(imageWidth * 1.2);
            }
            else
            {
                //等比例
                this.pic_MaxImage.Height = imageHeight;
                this.pic_MaxImage.Width = imageWidth;
            }        

            //设置窗体大小
            this.pic_MaxImage.Parent.Height = this.pic_MaxImage.Height + 100;
            this.pic_MaxImage.Parent.Width = this.pic_MaxImage.Width + 200;


            //设置图片位置
            int x = pic_MaxImage.Parent.Location.X + (pic_MaxImage.Parent.Width - pic_MaxImage.Width) / 2;
            int y = pic_MaxImage.Parent.Location.Y + (pic_MaxImage.Parent.Height - pic_MaxImage.Height) / 2;
            this.pic_MaxImage.Location = new System.Drawing.Point(x, y);
        }

        private void bt_save_Click(object sender, EventArgs e)
        {
            Image image = this.pic_MaxImage.Image;
            //用户自由选择指定路径保存文件
            SaveFileDialog savedialog = new SaveFileDialog();
            savedialog.FileName = "保存图片";
            savedialog.Filter = "Png 图片|*.png|Jpg 图片|*.jpg|Bmp 图片|*.bmp|Gif 图片|*.gif";
            savedialog.FilterIndex = 0;        
            savedialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
            savedialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//初始默认目录，桌面
            savedialog.CheckPathExists = true;//检查目录
            savedialog.FileName = "图片_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");//设置默认文件名
            if (savedialog.ShowDialog() == DialogResult.OK)
            {
                image.Save(savedialog.FileName);// image为要保存的图片
                MessageBox.Show(this, "图片保存成功！", "提示");
            }
        }
    }
}
